package com.hdp.kotlin_samples.base

/**
 * author:hdp
 * on:2019/6/11:21:06
 * desc:描述信息
 *
 * kotlin中的for循环
 *
 * for 语句和java相比，更加灵活，简洁
 */
class tips3_for {

    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
//            test_for1()
//            test_for2()
//            test_for3()
            test_for_4()
        }

        private fun test_for1() {
            println("===============test_for1_start===============")
            val marks = arrayOf(80, 85, 60, 90, 70)
            for (item in marks) {
                println(item)
            }
            println("===============test_for1_end===============")
        }

        /**
         * 使用索引迭代
         */
        private fun test_for2() {
            println("===============test_for2_start===============")
            val marks = arrayOf(80, 85, 60, 90, 70)
            for (item in marks.indices) {
                println(marks[item])
            }
            println("===============test_for2_end===============")
        }

        /**
         * 范围迭代
         */
        private fun test_for3() {
            println("===============test_for3_start===============")
            print("for (i in 1..5) print(i) = ")
            for (i in 1..5) print(i)
            println()
            print("for (i in 5..1) print(i) = ")
            for (i in 5..1) print(i)             // prints nothing
            println()
            print("for (i in 5 downTo 1) print(i) = ")
            for (i in 5 downTo 1) print(i)
            println()
            print("for (i in 5 downTo 2) print(i) = ")
            for (i in 5 downTo 2) print(i)
            println()
            print("for (i in 1..5 step 2) print(i) = ")
            for (i in 1..5 step 2) print(i)
            println()
            print("for (i in 5 downTo 1 step 2) print(i) = ")
            for (i in 5 downTo 1 step 2) print(i)
            println()
            print("for (i in 1 until 5) print(i) = ")
            for (i in 1 until 5) print(i)     //不包含5
            println()
            println("===============test_for3_end===============")
        }

        /**
         * 使用解构进行迭代
         */
        private fun test_for_4() {
            val marks = arrayOf(80, 85, 60, 90, 70)
            for ((index, value) in marks.withIndex()) {
                println("the element at $index is $value")
            }
        }

    }
}

